Difference Between Pseudo-Elements and Pseudo-Classes in CSS
Pseudo-elements and pseudo-classes in CSS are both used to style elements, but they serve different purposes. Pseudo-elements target specific parts of an element, while pseudo-classes target an element based on its state or position.
Pseudo-elements (e.g., ::before, ::after, ::first-letter) style subparts of an element or insert content dynamically.
Pseudo-classes (e.g., :hover, :focus, :first-child) style an element based on its state or its relationship with siblings.
Pseudo-elements use double colons :: (though single colon : is also supported for legacy browsers), while pseudo-classes use a single colon :.
Pseudo-elements do not exist in the DOM; pseudo-classes reflect actual element states.
In this example, ::first-letter is a pseudo-element targeting part of the paragraph, while :hover is a pseudo-class targeting the link when the user hovers over it.
Use pseudo-elements to enhance content without adding extra HTML.
Use pseudo-classes to reflect user interaction or structural states.
Combine pseudo-classes and pseudo-elements when necessary, e.g., p:first-child::first-letter.
Ensure accessibility and test interactions across browsers.
How would you add a small asterisk after required form fields using CSS, and why wouldn't you use :required instead?
What happens if you try to apply a background color to ::first-line on a div with inline content? Why?
You're trying to style the first letter of a paragraph with ::first-letter, but it's not working—what are two common reasons this might fail?
A tooltip component using ::before for the arrow breaks when the parent has overflow: hidden—how would you debug and fix this without changing the HTML?
Your team’s design system uses :hover to show a dropdown, but it doesn’t work on touch devices—how would you adjust the CSS to handle both hover and focus states without duplicating styles?
A button with ::after content for an icon is misaligned on mobile—what CSS properties might be causing this, and how would you verify your fix?
You’re building a reusable card component that uses ::before for decorative borders—how would you ensure it doesn’t break when the card is nested inside a container with transform or filter styles?
A legacy component uses pseudo-elements for icons via content: url(...)—how would you migrate this to SVG icons without breaking existing layouts or accessibility?
Your CSS-in-JS library doesn’t support pseudo-elements well—how would you design a workaround that maintains performance and SSR compatibility?
Your company’s design system relies heavily on pseudo-elements for UI components across 50+ apps—how would you architect a migration to a token-based, component-driven approach without breaking legacy themes?
Pseudo-elements are used in critical accessibility features like screen reader announcements via content: attr(aria-label)—how would you evaluate the long-term maintainability and WCAG compliance of this pattern?
You’re designing a CSS architecture for a global platform with 100+ teams—how would you enforce consistent usage of pseudo-elements vs. real DOM elements to avoid performance regressions and accessibility gaps?